package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 品牌表 业务实现类
 *
 * @author atguigu
 * @since 2023-02-22
 */
@Service
public class BaseTrademarkServiceImpl extends ServiceImpl<BaseTrademarkMapper, BaseTrademark> implements BaseTrademarkService {

    @Autowired
    private BaseCategoryTrademarkService baseCategoryTrademarkService;

    /**
     * 品牌分页查询
     *
     * @param iPage
     * @return
     */
    @Override
    public IPage<BaseTrademark> getBaseTrademarkByPage(IPage<BaseTrademark> iPage) {
        //分页条件
        LambdaQueryWrapper<BaseTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(BaseTrademark::getUpdateTime);
        //调用业务层或者持久层对象分页方法
        return this.page(iPage, queryWrapper);
    }

    /**
     * 根据category3Id获取品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkListByCategoryId(Long category3Id) {
        //1.根据分类ID查询分类品牌关系表 得到该分类下品牌ID集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        //单独指定查询字段
        queryWrapper.select(BaseCategoryTrademark::getTrademarkId);
        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTrademarkService.list(queryWrapper);
        //2.根据品牌ID集合查询分类列表
        if (!CollectionUtils.isEmpty(baseCategoryTrademarks)) {
            List<Long> trademarkIdList = baseCategoryTrademarks.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            return this.listByIds(trademarkIdList);
        }
        return null;
    }


    /**
     * 根据分类ID查询该分类未关联品牌
     * @param category3Id 三级分类ID
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        List<BaseTrademark> list = this.getBaseMapper().getCurrentTrademarkList(category3Id);
        return list;
    }
}
